package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.dto.request.HotelOrderRequestDto;
import cn.buk.tms.dto.response.HotelOrderResponseDto;
import cn.buk.tms.hotel.sc.HotelOrderSearchCriteria;
import cn.buk.tms.hotel.service.HotelOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yfdai
 */
@RestController
@RequestMapping("/hotel")
public class HotelOrderController extends BaseController {

    private final HotelOrderService hotelOrderService;

    @Autowired
    public HotelOrderController(HotelOrderService hotelOrderService) {
        this.hotelOrderService = hotelOrderService;
    }

    /**
     * 搜索酒店订单
     */
    @RequestMapping("/orders")
    public CommonDto<HotelOrderResponseDto> searchHotelOrders(HttpServletRequest request,
                                                    @ModelAttribute("sc") HotelOrderSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        return hotelOrderService.searchHotelOrders2(enterpriseId, userId, sc);
    }

    /**
     * 获取酒店订单详情
     */
    @RequestMapping("/order/{orderId}")
    public HotelOrderResponseDto getHotelOrder(HttpServletRequest request,
                                                   @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        return hotelOrderService.getHotelOrderById(enterpriseId, userId, orderId);
    }

    /**
     * 修改酒店订单的预订渠道
     */
    @PostMapping("/order/{orderId}/channel")
    public JsonResult updateHotelOrderChannel(HttpServletRequest request,
                                               @PathVariable("orderId") final int orderId,
                                               @RequestParam("channel") final int channel
                                              ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.updateHotelOrderChannel(enterpriseId, username, orderId, channel);
    }

    /**
     * 创建酒店订单
     */
    @PostMapping(value = "/order", consumes = "application/json")
    public JsonResult createHotelOrder(HttpServletRequest request,
                                       @RequestBody HotelOrderRequestDto dto) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.createHotelOrder(enterpriseId, username, dto);
    }

    /**
     * 修改酒店订单的备注
     */
    @PostMapping(value = "/order/{orderId}/remark")
    public JsonResult updateHotelOrderRemark(HttpServletRequest request,
                                             @PathVariable("orderId") final int orderId,
                                             @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.updateHotelOrderRemark(enterpriseId, username, orderId, remark);
    }

    /**
     * 修改酒店订单的供应商
     */
    @PostMapping(value = "/order/{orderId}/supplier")
    public JsonResult updateHotelOrderSupplier(HttpServletRequest request,
                                             @PathVariable("orderId") final int orderId,
                                             @RequestParam("supplierId") int supplierId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.updateHotelOrderSupplier(enterpriseId, username, orderId, supplierId);
    }

    /**
     * 修改酒店订单的对外支付方式
     */
    @PostMapping(value = "/order/{orderId}/paymentMethod")
    public JsonResult updateHotelOrderPaymentMethod(HttpServletRequest request,
                                               @PathVariable("orderId") final int orderId,
                                               @RequestParam("paymentMethodId") int paymentMethodId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.updateHotelOrderPaymentMethod(enterpriseId, username, orderId, paymentMethodId);
    }

    /**
     * 修改订单的价格
     */
    @PostMapping(value = "/order/{orderId}/price")
    public JsonResult updateHotelOrderPrice(HttpServletRequest request,
                                          @PathVariable("orderId") int orderId,
                                          @RequestParam("price") double price,
                                          @RequestParam("serviceCharge") double serviceCharge,
                                          @RequestParam("cost") double cost
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.updateHotelOrderPrice(enterpriseId, username, orderId, price, cost, serviceCharge);
    }

    /**
     * 提交酒店订单
     */
    @PostMapping(value = "/order/{orderId}/submit")
    public JsonResult submitHotelOrder(HttpServletRequest request,
                                        @PathVariable("orderId") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.submitHotelOrder(enterpriseId, username, orderId);
    }

    /**
     * 审批酒店订单
     * @param denyCode 0-同意；其它-不同意
     * @param denyReason 不同意的原因，不同意时必填
     */
    @PostMapping(value = "/order/{id}/approve")
    public JsonResult approveFlightOrder(HttpServletRequest request,
                                         @PathVariable("id") int orderId,
                                         @RequestParam("denyCode") int denyCode,
                                         @RequestParam(value = "denyReason", required = false) String denyReason
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);
        return hotelOrderService.approveHotelOrder(enterpriseId, username, orderId, denyCode, denyReason);
    }

    /**
     * 开始处理酒店订单
     */
    @PostMapping(value = "/order/{orderId}/status/processing")
    public JsonResult processHotelOrder(HttpServletRequest request,
                                                     @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.processHotelOrder(enterpriseId, username, orderId);
    }

    /**
     * 酒店订单处理完毕
     * @param confirmNo  酒店确认单号
     */
    @PostMapping(value = "/order/{orderId}/status/finished")
    public JsonResult finishHotelOrder(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String confirmNo) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.finishHotelOrder(enterpriseId, username, orderId, confirmNo);
    }

    @PostMapping(value = "/order/{orderId}/status/canceled")
    public JsonResult cancelHotelOrder(HttpServletRequest request,
                                       @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String remark) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.cancelHotelOrder(enterpriseId, username, orderId, remark);
    }

    @RequestMapping(value = "/order/{orderId}/history")
    public List<OperationLog> searchHotelOrderHistory(HttpServletRequest request,
                                                      @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);

        return hotelOrderService.searchHotelOrderHistory(enterpriseId, orderId);
    }

    /**
     * 检查订单是否可定
     */
    @RequestMapping(value = "/order/{orderId}/checkAvail")
    public JsonResult checkHotelAvail(HttpServletRequest request,
                                       @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.checkSupplierHotelAvail(enterpriseId, username, orderId);
    }

    /**
     * 同步远端订单详情
     */
    @RequestMapping(value = "/order/{orderId}/syncOrderDetail")
    public JsonResult syncHotelOrderDetail(HttpServletRequest request,
                                      @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.syncSupplierHotelOrder(enterpriseId, username, orderId);
    }

    /**
     * 取消远端订单
     */
    @RequestMapping(value = "/order/{orderId}/cancelOrder")
    public JsonResult cancelHotelOrder(HttpServletRequest request,
                                           @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.cancelSupplierHotelOrder(enterpriseId, username, orderId);
    }

    /**
     * 创建远端订单
     */
    @RequestMapping(value = "/order/{orderId}/createOrder")
    public JsonResult createHotelOrder(HttpServletRequest request,
                                       @PathVariable("orderId") final int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return hotelOrderService.createSupplierHotelOrder(enterpriseId, username, orderId);
    }
}
